#include "packetdetaildialog.h"
#include "ui_packetdetaildialog.h"
#include "common.h"
#include <QFont>
#include <QScrollArea>
#include <QTextEdit>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>

PacketDetailDialog::PacketDetailDialog(const PacketInfo &packetInfo, QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::PacketDetailDialog)
{
    ui->setupUi(this);
    
    // 设置对话框属性
    setWindowTitle("数据包详情");
    resize(900, 600);
    
    // 设置标题
    setupTitle(packetInfo);
    
    // 1. 基本信息标签页
    QTextEdit *basicInfoTextEdit = new QTextEdit;
    basicInfoTextEdit->setReadOnly(true);
    basicInfoTextEdit->setFont(QFont("Monaco", 10));
    
    QString basicInfo;
    basicInfo += "=== 基本信息 ===\n";
    basicInfo += QString("序号: %1\n").arg(packetInfo.index);
    basicInfo += QString("时间戳: %1\n").arg(packetInfo.timestamp);
    basicInfo += QString("长度: %1 字节\n\n").arg(packetInfo.length);
    basicInfo += "=== 地址信息 ===\n";
    basicInfo += QString("源MAC: %1\n").arg(packetInfo.sourceMac);
    basicInfo += QString("目标MAC: %1\n").arg(packetInfo.destMac);
    basicInfo += QString("源IP: %1\n").arg(packetInfo.sourceIp);
    basicInfo += QString("目标IP: %1\n").arg(packetInfo.destIp);
    basicInfo += QString("协议: %1\n").arg(packetInfo.protocol);
    
    basicInfoTextEdit->setText(basicInfo);
    
    // 为UI中已有的基本信息标签页设置布局和控件
    QVBoxLayout *basicInfoLayout = new QVBoxLayout(ui->tabBasicInfo);
    basicInfoLayout->addWidget(basicInfoTextEdit);
    
    // 2. 协议详情标签页
    QTextEdit *protocolDetailTextEdit = new QTextEdit;
    protocolDetailTextEdit->setReadOnly(true);
    protocolDetailTextEdit->setFont(QFont("Monaco", 10));
    
    QString protocolDetail;
    protocolDetail += "=== 数据链路层 (以太网) ===\n";
    protocolDetail += packetInfo.linkLayerDetail + "\n\n";
    protocolDetail += "=== 网络层 (IP) ===\n";
    protocolDetail += packetInfo.networkLayerDetail + "\n\n";
    protocolDetail += "=== 传输层 ===\n";
    protocolDetail += packetInfo.transportLayerDetail + "\n\n";
    protocolDetail += "=== 应用层 ===\n";
    protocolDetail += packetInfo.applicationLayerDetail;
    
    protocolDetailTextEdit->setText(protocolDetail);
    
    // 为UI中已有的协议详情标签页设置布局和控件
    QVBoxLayout *protocolDetailLayout = new QVBoxLayout(ui->tabProtocolDetail);
    protocolDetailLayout->addWidget(protocolDetailTextEdit);
    
    // 3. 原始数据标签页
    QTextEdit *rawDataTextEdit = new QTextEdit;
    rawDataTextEdit->setReadOnly(true);
    rawDataTextEdit->setFont(QFont("Monaco", 10));
    
    // 使用真实捕获的原始数据
    QString rawDataView = createCombinedView(packetInfo.rawData);
    rawDataTextEdit->setText(rawDataView);
    
    // 为UI中已有的原始数据标签页设置布局和控件
    QVBoxLayout *rawDataLayout = new QVBoxLayout(ui->tabRawData);
    rawDataLayout->addWidget(rawDataTextEdit);
}

PacketDetailDialog::~PacketDetailDialog()
{
    delete ui;
}

// 格式化原始数据为十六进制
QString PacketDetailDialog::formatHexData(const QByteArray &data)
{
    QString hexData;
    int lineCount = 0;
    
    for (int i = 0; i < data.size(); i++) {
        if (i % 16 == 0) {
            if (i > 0) {
                hexData += "\n";
            }
            hexData += QString("%1: ").arg(i, 8, 16, QLatin1Char('0')).toUpper();
        } else if (i % 8 == 0) {
            hexData += "  ";
        }
        
        hexData += QString("%1 ").arg(static_cast<unsigned char>(data[i]), 2, 16, QLatin1Char('0')).toUpper();
    }
    
    return hexData;
}

// 格式化原始数据为ASCII
QString PacketDetailDialog::formatAsciiData(const QByteArray &data)
{
    QString asciiData;
    
    for (int i = 0; i < data.size(); i++) {
        unsigned char byte = static_cast<unsigned char>(data[i]);
        if (byte >= 32 && byte <= 126) {
            asciiData += QChar(byte);
        } else {
            asciiData += '.';
        }
        
        if ((i + 1) % 16 == 0 && i < data.size() - 1) {
            asciiData += "\n";
        }
    }
    
    return asciiData;
}

// 创建十六进制和ASCII的组合视图
QString PacketDetailDialog::createCombinedView(const QByteArray &data)
{
    QString combinedView;
    
    // 调试信息：显示数据大小
    combinedView += QString("数据大小: %1 字节\n\n").arg(data.size());
    
    // 如果没有数据，显示提示信息
    if (data.isEmpty()) {
        combinedView += "没有可用的原始数据。";
        return combinedView;
    }
    
    // 简单的十六进制视图
    combinedView += "=== 十六进制数据 ===\n";
    for (int i = 0; i < data.size(); i++) {
        if (i % 16 == 0) {
            if (i > 0) {
                combinedView += "\n";
            }
            combinedView += QString("%1: ").arg(i, 8, 16, QLatin1Char('0')).toUpper();
        } else if (i % 8 == 0) {
            combinedView += "  ";
        }
        
        combinedView += QString("%1 ").arg(static_cast<unsigned char>(data[i]), 2, 16, QLatin1Char('0')).toUpper();
    }
    
    combinedView += "\n\n=== ASCII 数据 ===\n";
    
    // ASCII视图
    for (int i = 0; i < data.size(); i++) {
        if (i % 16 == 0 && i > 0) {
            combinedView += "\n";
        }
        
        unsigned char byte = static_cast<unsigned char>(data[i]);
        if (byte >= 32 && byte <= 126) {
            combinedView += QChar(byte);
        } else {
            combinedView += '.';
        }
    }
    
    return combinedView;
}

// 设置对话框标题
void PacketDetailDialog::setupTitle(const PacketInfo &packetInfo)
{
    setWindowTitle(QString("数据包 #%1 - %2").arg(packetInfo.index).arg(packetInfo.protocol));
}

// 设置基本信息
void PacketDetailDialog::setupBasicInfo(const PacketInfo &packetInfo)
{
    // 已经在构造函数中实现
}

// 设置协议详情
void PacketDetailDialog::setupProtocolDetails(const PacketInfo &packetInfo)
{
    // 已经在构造函数中实现
}

// 设置原始数据视图
void PacketDetailDialog::setupRawDataView(const QByteArray &data)
{
    // 已经在构造函数中实现
}